package ch01.NodeGarden {
	/**
	 * 检测不只是为了碰撞，例子一
	 * @author jianping.shenjp
	 */
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.display.StageAlign;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.utils.getTimer;
	import tools.Ball;

	[SWF(backgroundColor=0x000000)]
	public class NodeGardenLines extends Sprite {
		private var particles:Array;
		private var numParticles:uint = 500;
		private var minDist:Number = 50;
		private var springAmount:Number = .001;
		private var _numChecks:int = 0;

		public function NodeGardenLines(){
			init();
		}

		private function init():void {
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			particles = new Array();
			for (var i:uint = 0; i < numParticles; i++){
				var particle:Ball = new Ball(3, 0xffffff);
				particle.x = Math.random() * stage.stageWidth;
				particle.y = Math.random() * stage.stageHeight;
				particle.vx = Math.random() * 6 - 3;
				particle.vy = Math.random() * 6 - 3;
				addChild(particle);
				particles.push(particle);
			}
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}

		private function onEnterFrame(event:Event):void {
			_numChecks = 0
			var startTime:int = getTimer();
			graphics.clear();
			for (var i:uint = 0; i < numParticles; i++){
				var particle:Ball = particles[i];
				particle.x += particle.vx;
				particle.y += particle.vy;
				if (particle.x > stage.stageWidth){
					particle.x = 0;
				} else if (particle.x < 0){
					particle.x = stage.stageWidth;
				}
				if (particle.y > stage.stageHeight){
					particle.y = 0;
				} else if (particle.y < 0){
					particle.y = stage.stageHeight;
				}
			}
			for (i = 0; i < numParticles - 1; i++){
				var partA:Ball = particles[i];
				for (var j:uint = i + 1; j < numParticles; j++){
					var partB:Ball = particles[j];
					spring(partA, partB);
				}
			}
			trace("3:Elapsed:", (getTimer() - startTime), "测试次数:", _numChecks);
		}

		private function spring(partA:Ball, partB:Ball):void {
			_numChecks++;
			var dx:Number = partB.x - partA.x;
			var dy:Number = partB.y - partA.y;
			var dist:Number = Math.sqrt(dx * dx + dy * dy);
			if (dist < minDist){
				graphics.lineStyle(1, 0xffffff, 1 - dist / minDist);
				graphics.moveTo(partA.x, partA.y);
				graphics.lineTo(partB.x, partB.y);
				var ax:Number = dx * springAmount;
				var ay:Number = dy * springAmount;
				partA.vx += ax;
				partA.vy += ay;
				partB.vx -= ax;
				partB.vy -= ay;
			}
		}
	}

}